package com.sm.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.sm.model.*;
import com.sm.service.*;
import com.sm.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/likeDynamic")
public class LikeDynamicController {

    @Autowired
    private UserService userService;
    @Autowired
    private LikeDynamicService likeDynamicService;
    @Autowired
    private DynamicCommentService dynamicCommentService;
    @Autowired
    private DynamicService dynamicService;

    /**
     * 点赞动态
     */
    @ResponseBody
    @RequestMapping("/like")
    public Msg insertLikeDynamic(Integer userId,Integer dynamicId) {
        List<LikeDynamic> exist = likeDynamicService.isExist(dynamicId, userId);
        Dynamic dynamics = dynamicService.selectDynamicByDaynamicId(dynamicId);
        Integer likeNum = dynamics.getLikeNum();
        Dynamic dynamic = new Dynamic();
        if (ObjectUtil.isEmpty(exist)){
            //数据库里不存在，进行点赞操作(新增一条点赞记录和点赞次数+1)
            LikeDynamic likeDynamic = new LikeDynamic();
            likeDynamic.setLikeUser(userId);
            likeDynamic.setDynamicId(dynamicId);
            likeDynamic.setDynamicUser(dynamics.getUserId());
            likeDynamic.setCreateTime(new Date());
            likeDynamicService.insertLikeDynamic(likeDynamic);
            dynamic.setLikeNum(likeNum+1);
            dynamic.setDynamicId(dynamicId);
            int i = dynamicService.updateDynamic(dynamic);
            return Msg.success().add("msg", "点赞记录添加成功");
        }else {
            //数据库里存在，进行取消点赞操作(删除掉此条点赞记录和点赞次数-1)
            likeDynamicService.deleteOne(dynamicId,userId);
            dynamic.setLikeNum(likeNum-1);
            dynamic.setDynamicId(dynamicId);
            int i = dynamicService.updateDynamic(dynamic);
            return Msg.success().add("msg","点赞记录删除成功");
        }
    }

    /**
     * 评论/回复动态
     */
    @ResponseBody
    @RequestMapping("/commentDynamic")
    public Msg commentDynamic(@RequestParam("dynamicId") Integer dynamicId,
                              @RequestParam("commentUser") Integer commentUser,
                              @RequestParam("content") String content,
                              @RequestParam("contentGrade") Integer contentGrade,
                              @RequestParam(value = "fatherComment",required = false) Integer fatherComment,
                              @RequestParam(value = "topId",required = false)Integer topId) {

        DynamicComment dynamicComment = new DynamicComment();
        dynamicComment.setDynamicId(dynamicId);
        dynamicComment.setCommentUser(commentUser);
        User user = userService.selectAvatarAndNickName(commentUser);
        if (ObjectUtil.isNotEmpty(user)){
            //评论人的昵称
            dynamicComment.setCommentName(user.getNickName());
        }
        if (StrUtil.isBlank(content)) {
            return Msg.fail().add("msg", "评论失败");
        }
        dynamicComment.setTopId(topId);
        dynamicComment.setContent(content);//评论内容
        dynamicComment.setContentGrade(contentGrade);//评论级别（是对动态的评论还是对评论的回复）
        if (fatherComment != null){
            dynamicComment.setFatherComment(fatherComment);
        }
        DynamicComment dynamicComment1 = dynamicCommentService.selectById(fatherComment);
        if (ObjectUtil.isNotEmpty(dynamicComment1)){
            dynamicComment.setFatherName(dynamicComment1.getCommentName());//被评论人的名字
        }
        dynamicComment.setCreateTime(new Date());
        int i = dynamicCommentService.commentDynamic(dynamicComment);
        if (i == 1) {
            changeCommentNum(dynamicService.selectDynamicByDaynamicId(dynamicId));
            return Msg.success().add("msg", "评论成功");
        }
        return Msg.fail().add("msg", "评论失败");
    }

    /**
     * 更改动态的评论数
     */
    public void changeCommentNum(Dynamic dynamic){
        Dynamic dy = new Dynamic();
        if (ObjectUtil.isNotEmpty(dynamic)){
            dy.setCommentNum(dynamic.getCommentNum()+1);
            dy.setDynamicId(dynamic.getDynamicId());
            dynamicService.updateDynamic(dy);
        }
    }

    /**
     * 删除点赞
     *
     * @param dynamicId
     * @return
     */
    @RequestMapping("/deleteCommentByDynamic")
    @ResponseBody
    public Msg deleteCommentByDynamic(@RequestParam("dynamicId") Integer dynamicId) {

        if (dynamicId == null) {
            return Msg.fail().add("msg", "参数不为空！");
        }
        likeDynamicService.deleteComment(dynamicId);

        return Msg.success().add("msg", "删除成功");

    }

    /**
     * 通过动态id查询点赞
     */
    @RequestMapping("/selectLikeDynamic")
    @ResponseBody
    public Msg selectLikeDynamic(@RequestParam("dynamicId") Integer dynamicId) {

        List<LikeDynamic> likeDynamics = likeDynamicService.selectLikeDynamicById(dynamicId);
        return Msg.success().add("likeDynamics",likeDynamics);

    }

}
